package org.makcro.sms.services.chat.web.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.makcro.sms.services.chat.web.client.mvc.AgentsMessagesController;
import org.makcro.sms.services.chat.web.client.mvc.AllMessagesController;
import org.makcro.sms.services.chat.web.client.mvc.AnswerController;
import org.makcro.sms.services.chat.web.client.mvc.BulkTabController;
import org.makcro.sms.services.chat.web.client.mvc.LoginController;
import org.makcro.sms.services.chat.web.client.mvc.MainController;
import org.makcro.sms.services.chat.web.client.mvc.NewMessagesController;
import org.makcro.sms.services.chat.web.client.mvc.SendBulkController;
import org.makcro.sms.services.chat.web.client.mvc.SubscribersTabController;
import org.makcro.sms.services.chat.web.client.services.login.LoginService;
import org.makcro.sms.services.chat.web.client.services.login.LoginServiceAsync;
import org.makcro.sms.services.chat.web.client.services.message.BulkService;
import org.makcro.sms.services.chat.web.client.services.message.BulkServiceAsync;
import org.makcro.sms.services.chat.web.client.services.message.MessageService;
import org.makcro.sms.services.chat.web.client.services.message.MessageServiceAsync;
import org.makcro.sms.services.chat.web.client.services.subscriber.SubscriberService;
import org.makcro.sms.services.chat.web.client.services.subscriber.SubscriberServiceAsync;
import org.makcro.sms.services.chat.web.shared.dto.HistoryItemDto;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Theme;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Chat service entry point.
 * 
 * @author makcro
 * 
 */
public class SmsChat implements EntryPoint {

	private Logger	logger	= Logger.getLogger( "ChatService" );

	public void onModuleLoad() {

		GXT.setDefaultTheme( Theme.BLUE, false );

		initServices();

		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.addController( new LoginController() );
		logger.log( Level.FINE, "Registered Login controller" );
		dispatcher.addController( new MainController() );
		logger.log( Level.FINE, "Registered Main controller" );
		dispatcher.addController( new NewMessagesController() );
		logger.log( Level.FINE, "Registered NewMessagesController controller" );
		dispatcher.addController( new AllMessagesController() );
		logger.log( Level.FINE, "Registered AllMessagesController controller" );
		dispatcher.addController( new AgentsMessagesController() );
		logger.log( Level.FINE, "Registered AgentMessagesController controller" );
		dispatcher.addController( new AnswerController() );
		logger.log( Level.FINE, "Registered Answer controller" );
		dispatcher.addController( new SendBulkController() );
		logger.log( Level.FINE, "Registered SendBulk controller" );
		dispatcher.addController( new BulkTabController() );
		dispatcher.addController( new SubscribersTabController() );

		dispatcher.dispatch( AppEvents.ShowLoginDialog );
	}

	private void initServices() {
		LoginServiceAsync loginService = GWT.create( LoginService.class );
		final MessageServiceAsync messageService = GWT.create( MessageService.class );
		BulkServiceAsync bulkService = GWT.create( BulkService.class );
		SubscriberServiceAsync subscriberService = GWT.create( SubscriberService.class );

		// create rcp proxies
		RpcProxy<PagingLoadResult<HistoryItemDto>> agentsMessagesRpcProxy = new RpcProxy<PagingLoadResult<HistoryItemDto>>() {

			@Override
			protected void load(Object loadConfig, AsyncCallback<PagingLoadResult<HistoryItemDto>> callback) {
				messageService.getCurrentAgentsMessages( (PagingLoadConfig) loadConfig, callback );
			}
		};

		RpcProxy<PagingLoadResult<HistoryItemDto>> allAnsweredMessagesRpcProxy = new RpcProxy<PagingLoadResult<HistoryItemDto>>() {

			@Override
			protected void load(Object loadConfig, AsyncCallback<PagingLoadResult<HistoryItemDto>> callback) {
				messageService.getAllHistory( (PagingLoadConfig) loadConfig, callback );

			}
		};

		Registry.register( Constants.Services.LoginService, loginService );
		logger.log( Level.INFO, "Registered Login service" );

		Registry.register( Constants.Services.MessageService, messageService );
		Registry.register( Constants.Services.AGENTS_MESSAGES_RPC_PROXY, agentsMessagesRpcProxy );
		Registry.register( Constants.Services.ALL_ANSWERED_MESSAGES_PROXY, allAnsweredMessagesRpcProxy );
		logger.log( Level.INFO, "Registered Message service" );

		Registry.register( Constants.Services.BULK_SERVICE, bulkService );
		logger.log( Level.INFO, "Registered Bulk service" );

		Registry.register( Constants.Services.SUBSCRIBER_SERVICE, subscriberService );
	}

}
